package com.jintoufs.web.action.baseInfo;

import com.alibaba.fastjson.JSON;
import com.jintoufs.domain.baseinfo.Coin;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.baseinfo.CoinService;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoemo
 */
@Controller
@Scope("prototype")
@RequestMapping("/coin")
public class CoinAction extends SupportAction {
    @Resource
    private CoinService coinService;

    @RequestMapping("/list")
    public String list(Model model) throws IOException {
        List<Map<String, Object>> list = coinService.queryByParams(null);
        model.addAttribute("listEntity", list);
        return "baseinfo/coin-list";
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "修改币种")
    @ResponseBody
    public AjaxMsg oper(HttpServletResponse response, String oper, Coin coin) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            if (oper.equals("del")) {
                coinService.deleteByPrimaryKey(coin.getId());
            } else {
                if (oper.equals("edit")) {
                    coinService.updateByPrimaryKey(coin);
                    ajaxMsg.setCode("200");
                    ajaxMsg.setMessage("更新成功");
                } else if (oper.equals("add")) {
                    ajaxMsg=coinService.insert(coin);
                } else {
                    ajaxMsg.setCode(AjaxMsg.FAILURE);
                    ajaxMsg.setMessage("操作代码错误！系统只提供增加/修改/删除功能。");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("添加失败");
        }
        return ajaxMsg;
    }

    /**
     * input.do?id= 进入类目编辑页面 <br>
     * input.do 进入类目新增页面（不包含选中父类目）
     *
     * @param id    类目Id
     * @param model
     * @return vm/category/input.vm
     */
    @RequestMapping("input")
    public String input(Integer id, Model model) {
        Coin result = null;
        String title;

        // 是否为编辑保存
        String isEdit;

        // 修改
        if (id != null && id != 0) {
            title = "修改";
            result = coinService.selectByPrimaryKey(id);
            isEdit = "edit";
        } else {
            title = "新增";
            isEdit = "add";
        }

        model.addAttribute("isEdit", isEdit);
        model.addAttribute("result", result);
        model.addAttribute("title", title);

        return "baseinfo/coin-input";
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除虚拟币")
    public void delete(HttpServletResponse response, Integer[] ids) {
        for (Integer id : ids) {
            coinService.deleteByPrimaryKey(id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

}
